const App = getApp()

Page({
  data: {
    address: {


      items: [
        {
          name: '李小姐',
          tel: '13500220022',
          address: '东城区东四北大街',
          is_def: 0

        },
        {
          name: '张先生',
          tel: '13500220022',
          address: '东城区国子监',
          is_def: 1
        }
      ],
      params: {
        page: 1,
        limit: 10,
      },
      paginate: { total: 2 }



    },
    prompt: {
      hidden: !0,
      icon: '../../../image/empty.png',
      title: '还没有收货地址呢',
      text: '暂时没有相关数据',
    },
  },
  onLoad() {
    //this.address = App.HttpResource('/address/:id', {id: '@id'})
    //this.onPullDownRefresh()
  },
  initData() {
    this.setData({
      address: {
        items: [],
        params: {
          page: 1,
          limit: 10,
        },
        paginate: {}
      }
    })
  },
  toAddressEdit(e) {
    console.log(e)
    App.WxService.navigateTo('/pages/address/edit/index', {
      id: e.currentTarget.dataset.id
    })
  },

  /**去增加收货地址页面 */
  toAddressAdd(e) {
    console.log(e)
    App.WxService.navigateTo('/pages/address/add/index')
  },

  setDefalutAddress(e) {
    const id = e.currentTarget.dataset.id
    App.HttpService.setDefalutAddress(id)
      .then(res => {
        const data = res.data
        console.log(data)
        if (data.meta.code == 0) {
          this.onPullDownRefresh()
        }
      })
  },


  getList() {
    const address = this.data.address
    const params = address.params

    console.log("address>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + address)


    //App.HttpService.getAddressList(params)
    this.address.queryAsync(params)
      .then(res => {
        const data = res.data
        console.log(data)
        if (data.meta.code == 0) {
          address.items = [...address.items, ...data.data.items]
          address.paginate = data.data.paginate
          address.params.page = data.data.paginate.next
          address.params.limit = data.data.paginate.perPage
          this.setData({
            address: address,
            'prompt.hidden': address.items.length,
          })
        }
      })
  },
  onPullDownRefresh() {
    console.info('onPullDownRefresh')
    this.initData()
    this.getList()
  },
  onReachBottom() {
    console.info('onReachBottom')
    if (!this.data.address.paginate.hasNext) return
    this.getList()
  },
})